home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / util_lib.c < prev    next >
C/C++ Source or Header  |  1994-08-03  |  14KB  |  484 lines

  1. #include "ctdl.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. CONFIG    cfg;                   /* A buncha variables           */
  6. LogTable  *logTab;               /* RAM index of pippuls         */
  7. NetTable  *netTab;               /* RAM index of nodes           */
  8. rTable    *roomTab;              /* RAM index of rooms           */
  9. EVENT     *EventTab = NULL;
  10. char             *indexTable = "ctdlTabl.sys";
  11. struct floor     *FloorTab;
  12. int              TopFloor;
  13. extern char *R_W_ANY;
  14.  
  15. logBuffer logBuf;                       /* Log buffer of a person       */
  16. logBuffer logTmp;                       /* Useful global buffer         */
  17. int       thisLog;                      /* entry currently in logBuf    */
  18. FILE      *logfl;                       /* log file descriptor          */
  19. static struct
  20.   {
  21.   int checkMark;                      /* rudimentary integrity        */
  22.   int cfgSize;                        /* sizeof cfg                   */
  23.   int logTSize;                       /* logtab size                  */
  24.   int endMark;                        /* another integrity check      */
  25.  
  26.   }  integrity;
  27. extern char *R_W_ANY;
  28. void *FindServes();
  29. SListBase Serves =
  30.   {
  31.   NULL, FindServes, NULL, free, NULL
  32.  
  33.   };
  34.  
  35. #define CHKM    7       /* major release        */
  36. #define ENDM    8
  37.  
  38. void getLog(logBuffer *lBuf, int n)
  39. {
  40.     long int s, r;
  41.  
  42.     if (lBuf == &logBuf)   thisLog      = n;
  43.  
  44.     r = LB_TOTAL_SIZE;                  /* To get away from overflows   */
  45.     s = n * r;                          /* This should be offset        */
  46.     n *= 3;
  47.  
  48.     fseek(logfl, s, 0);
  49.  
  50.     if (fread((char *)lBuf, LB_SIZE, 1, logfl) != 1)
  51.       {
  52.       printf("getLog: Could not read User Log entry: %d\n",n);
  53.       }
  54.     else
  55.       {
  56.       crypte(lBuf, LB_SIZE, n);           /* decode buffer    */
  57.       if (fread((char *)lBuf->lbgen, GEN_BULK, 1, logfl) != 1)
  58.         {
  59.         printf("?getLog-read fail//EOF detected (2)!");
  60.         };
  61.       if (fread((char *)lBuf->lbMail, MAIL_BULK, 1, logfl) != 1)
  62.         {
  63.         printf("getLog-read fail//EOF detected (3)!");
  64.         };
  65.       };
  66.   }
  67. /************************************************************************/
  68. /*      readSysTab() restores state of system from CTDLTABL.SYS         */
  69. /*      returns:        TRUE on success, else FALSE                     */
  70. /*        destroys CTDLTABL.TAB after read, to prevent erroneous re-use */
  71. /*        in the event of a crash.                                      */
  72. /*                                                                      */
  73. /*      MS-DOS fun: Here's the map --                                   */
  74. /*      Word 1 == sizeof cfg                                            */
  75. /*      Word 2 == sizeof logTab                                         */
  76. /*      Word 3 == sizeof roomTab                                        */
  77. /*      Word 4 -- thru x == cfg contents                                */
  78. /*      x -- y == logTab                                                */
  79. /*      y -- z == roomTab                                               */
  80. /*      z -- a == netTab                                                */
  81. /*      EOF                                                             */
  82. /************************************************************************/
  83. char readSysTab(char kill, char showMsg)
  84.   {
  85.   FILE *fd;
  86.   extern char *READ_ANY;
  87.   int         rover;
  88.   long        bytes;
  89.   SYS_FILE    name;
  90.   char        caller;
  91.   label       temp;
  92.   caller = cfg.weAre;
  93.   if ((fd = fopen(indexTable, READ_ANY)) == NULL)
  94.     {
  95.     printf("?no %s! could not open", indexTable);    /* Tsk, tsk! */
  96.     return(FALSE);
  97.  
  98.     }
  99.   if (fread((char *)&integrity, sizeof integrity, 1, fd) != 1)
  100.     {
  101.     printf("Unable to read the integrity data\n");
  102.     return FALSE;
  103.  
  104.     }
  105.   if (     integrity.checkMark != CHKM ||
  106.   integrity.endMark != ENDM ||
  107.   integrity.cfgSize != sizeof cfg)
  108.     {
  109.     printf("Integrity mismatch.  Old ctdltabl.sys?\n");
  110.     return(FALSE);
  111.  
  112.     }
  113.   if (!common_read(&cfg, (sizeof cfg), 1, fd, showMsg))
  114.   return FALSE;
  115.   /* Allocations for dynamic parameters */
  116.   logTab = (LogTable *) GetDynamic(integrity.logTSize);
  117.   roomTab = (rTable *) GetDynamic(MAXROOMS * (sizeof (*roomTab)));
  118.   if (cfg.netSize)
  119.   netTab = (NetTable *) GetDynamic(sizeof (*netTab) * cfg.netSize);
  120.   else
  121.   netTab = NULL;
  122.   if (cfg.EvNumber)
  123.   EventTab  = (EVENT *)
  124.   GetDynamic(sizeof (*EventTab) * cfg.EvNumber);
  125.   /* "- 1" is kludge */
  126.   if (integrity.logTSize != sizeof (*logTab) * cfg.MAXLOGTAB)
  127.     {
  128.     printf("Integrity check: size mismatch on logtable\n");
  129.     return(FALSE);
  130.  
  131.     }
  132.   if (!common_read(logTab, integrity.logTSize, 1, fd, showMsg))
  133.   return FALSE;
  134.   if (!common_read(roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd, showMsg))
  135.   return FALSE;
  136.   if (cfg.netSize)
  137.     {
  138.     for (rover = 0; rover < cfg.netSize; rover++)
  139.       {
  140.       if (!common_read(&netTab[rover], NT_SIZE, 1, fd, showMsg))
  141.       return FALSE;
  142.       netTab[rover].netTRooms =
  143.       (SharedRoom *) GetDynamic(SR_BULK);
  144.       if (!common_read(netTab[rover].netTRooms, SR_BULK, 1, fd, showMsg))
  145.       return FALSE;
  146.  
  147.       }
  148.  
  149.     }
  150.   if (cfg.EvNumber)
  151.     {
  152.     if (!common_read(EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd,
  153.     showMsg))
  154.     return FALSE;
  155.  
  156.     }
  157.   for (rover = 0; rover < cfg.DomainHandlers; rover++)
  158.     {
  159.     if (!common_read(temp, NAMESIZE, 1, fd, showMsg))
  160.       {
  161.       return FALSE;
  162.  
  163.       }
  164.     AddData(&Serves, strdup(temp), NULL, FALSE);
  165.  
  166.     }
  167.   fclose(fd);
  168.   makeSysName(name, "ctdlflr.sys", &cfg.floorArea);
  169.   if ((fd = fopen(name, R_W_ANY)) == NULL)
  170.     {
  171.     if (caller != CONFIGUR)
  172.       {
  173.       printf("No floor table! could not open it");
  174.       return FALSE;
  175.  
  176.       }
  177.  
  178.     }
  179.   else
  180.     {
  181.     totalBytes(&bytes, fd);
  182.     FloorTab = (struct floor *) GetDynamic((int) bytes);
  183.     if (fread((char *)FloorTab, (int) bytes, 1, fd) != 1)
  184.       {
  185.       printf("problem reading floor tab");
  186.       fclose(fd);
  187.       if (caller != CONFIGUR) return FALSE;
  188.  
  189.       }
  190.     else
  191.       {
  192.       fclose(fd);
  193.       TopFloor = (int) bytes/sizeof(*FloorTab);
  194.  
  195.       }
  196.  
  197.     }
  198.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  199.   return(TRUE);
  200.  
  201.   }
  202. /************************************************************************/
  203. /*      common_read() reads in from file the important stuff            */
  204. /*      returns:        TRUE on success, else FALSE                     */
  205. /************************************************************************/
  206. static int common_read(void *block, int size, int elements, FILE *fd,
  207. char showMsg)
  208.   {
  209.   if (size == 0) return TRUE;
  210.   if (fread((char *)block, size, elements, fd) != 1)
  211.     {
  212.     printf("common_read: Unable to read data from file\n");
  213.     return FALSE;
  214.  
  215.     };
  216.   return TRUE;
  217.  
  218.   }
  219. void *special_GetDynamic(unsigned size, char *file, int line)
  220.   {
  221.   void *temp;
  222.   char msg[80];
  223.   if (size == 0) return NULL; /* Simplify code                */
  224.   if ((temp = (void *)malloc(size)) == NULL)
  225.     {
  226.     printf("Request for %u bytes failed.\n", size);
  227.     exit(100);
  228.     }
  229.   memset(temp, 0, size);
  230.   return temp;
  231.  
  232.   }
  233.  
  234. void crypte(void *buf, unsigned len, unsigned seed)
  235.   {
  236.   static AN_UNSIGNED *b;      /* Make this static for speed (I guess),*/
  237.   static  int c, s;           /* since register variables not around  */
  238.   if( cfg.cryptSeed == 0)return;
  239.   seed        = (seed + cfg.cryptSeed) & 0xFF;
  240.   b           = (AN_UNSIGNED *)buf;
  241.   c           = len;
  242.   s           = seed;
  243.   for (;  c;  c--)
  244.     {
  245.     *b++   ^= s;
  246.     s       = (s + CRYPTADD)  &  0xFF;
  247.  
  248.     }
  249.  
  250.   }
  251.  
  252. UNS_16 hash(char *str)
  253. {
  254.     UNS_16  h, shift;
  255.  
  256.     for (h=shift=0;  *str;  shift=(shift+1)&7, str++) {
  257.         h ^= (toUpper(*str)) << shift;
  258.     }
  259.     return h;
  260. }
  261. /************************************************************************/
  262. /*    FindServes() find the server function                */
  263. /************************************************************************/
  264. static void *FindServes(char *name, char *target)
  265.   {
  266.   return (strCmpU(name, target) == SAMESTRING) ? name : NULL;
  267.  
  268.   }
  269. static FILE *fd;
  270. /************************************************************************/
  271. /*      writeSysTab() saves state of system in CTDLTABL.SYS             */
  272. /*      returns:        TRUE on success, else ERROR                     */
  273. /*      See readSysTab() to see what the CTDLTABL.SYS map looks like.   */
  274. /************************************************************************/
  275. int writeSysTab()
  276.   {
  277.   void WriteServers();
  278.   extern char   *WRITE_ANY;
  279.   int           rover;
  280.   if ((fd = fopen(indexTable, WRITE_ANY)) == NULL)
  281.     {
  282.     printf("?can't make %s", indexTable);
  283.     return(ERROR);
  284.  
  285.     }
  286.   /* Write out some key stuff so we can detect bizarreness: */
  287.   integrity.checkMark = CHKM;
  288.   integrity.endMark = ENDM;
  289.   integrity.cfgSize = sizeof cfg;
  290.   integrity.logTSize = sizeof (*logTab) * cfg.MAXLOGTAB;
  291.   fwrite((char *)&integrity, (sizeof integrity), 1, fd);
  292.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  293.   fwrite((char *)&cfg, (sizeof cfg), 1, fd);
  294.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  295.   fwrite((char *)logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd);
  296.   fwrite((char *)roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd);
  297.   for (rover = 0; rover < cfg.netSize; rover++)
  298.     {
  299.     fwrite((char *)&netTab[rover], NT_SIZE, 1, fd);
  300.     fwrite((char *)netTab[rover].netTRooms, SR_BULK, 1, fd);
  301.  
  302.     }
  303.   if (cfg.EvNumber)
  304.   fwrite((char *)EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd);
  305.   RunList(&Serves, WriteServers);
  306.   fclose(fd);
  307.   return(TRUE);
  308.  
  309.   }
  310. /************************************************************************/
  311. /*    WriteServers() writes a server out to ctdltabl.sys        */
  312. /************************************************************************/
  313. static void WriteServers(char *name)
  314.   {
  315.   fwrite((char *)name, NAMESIZE, 1, fd);
  316.  
  317.   }
  318.  
  319. void putLog(logBuffer *lBuf, int n)
  320. {
  321.     long int s, r;
  322.  
  323.     r = LB_TOTAL_SIZE;
  324.     s = n * r;
  325.     n   *= 3;
  326.  
  327.     crypte(lBuf, LB_SIZE, n);         /* encode buffer        */
  328.  
  329.     if (cfg.weAre != CONFIGUR)        /* No need if configuring         */
  330.         fseek(logfl, s, 0);
  331.  
  332.     if (fwrite((char *)lBuf, LB_SIZE, 1, logfl) != 1) {
  333.         crashout("?putLog-write fail (1)!");
  334.     }
  335.  
  336.     if (fwrite((char *)lBuf->lbgen, GEN_BULK, 1, logfl) != 1) {
  337.         crashout("?putLog-write fail (2)!");
  338.     }
  339.  
  340.     if (fwrite((char *)lBuf->lbMail, MAIL_BULK, 1, logfl) != 1) {
  341.         crashout("?putLog-write fail (3)!");
  342.     }
  343.  
  344.     crypte(lBuf, LB_SIZE, n);         /* encode buffer        */
  345.  
  346.     fflush(logfl);
  347. }
  348. /************************************************************************/
  349. /*      openFile() opens one of the .sys files.                         */
  350. /************************************************************************/
  351. void openFile(char *filename, FILE **fd)
  352.   {
  353.   /* We use fopen here rather than safeopen for link reasons */
  354.   if ((*fd = fopen(filename, R_W_ANY)) == NULL)
  355.     {
  356.     printf("?no %s, cannot open it", filename);
  357.     exit(SYSOP_EXIT);
  358.  
  359.     }
  360.  
  361.   }
  362. /************************************************************************/
  363. /*      PersonExists() check to see if the given name is valid for mail */
  364. /************************************************************************/
  365. int PersonExists(char *name)
  366. {
  367.     int result;
  368.  
  369.     result = findPerson(name, &logTmp);
  370.     if (result != ERROR) strCpy(name, logTmp.lbname);
  371.     return result;
  372. }
  373. /************************************************************************/
  374. /*      findPerson() loads log record for named person.                 */
  375. /*      RETURNS: ERROR if not found, else log record #                  */
  376. /************************************************************************/
  377. int findPerson(char *name, logBuffer *lBuf)
  378. {
  379.     int  h, i, foundIt, logNo;
  380.  
  381.     if (strLen(name) == 0) return ERROR;
  382.  
  383.     if (strCmpU(name, "Citadel") != SAMESTRING) {
  384.         h   = hash(name);
  385.         for (foundIt = i = 0;  i < cfg.MAXLOGTAB && !foundIt;  i++) {
  386.             if (logTab[i].ltnmhash == h) {
  387.                 getLog(lBuf, logNo = logTab[i].ltlogSlot);
  388.                 if (lBuf->lbflags.L_INUSE &&
  389.                         strCmpU(name, lBuf->lbname) == SAMESTRING) {
  390.                     foundIt = TRUE;
  391.                 }
  392.             }
  393.         }
  394.     }
  395.     else foundIt = FALSE;
  396.     if (!foundIt)    return ERROR;
  397.     else             return logNo;
  398. }
  399.  
  400. /*
  401. * lbyte()
  402. *
  403. * This function finds the 0 byte of a string, returns pointer to it...
  404. */
  405. char *lbyte(char *l)
  406.   {
  407.   while (*l) l++;
  408.   return l;
  409.  
  410.   }
  411. /*
  412. * NormStr()
  413. *
  414. * This function Deletes leading trailing blanks etc.
  415. */
  416. void NormStr(char *s)
  417.   {
  418.   char *pc;
  419.   pc = s;
  420.   /* find end of string   */
  421.   while (*pc)
  422.     {
  423.     if (*pc < ' ')   *pc = ' ';   /* zap tabs etc... */
  424.     pc++;
  425.  
  426.     }
  427.   /* no trailing spaces: */
  428.   while (pc>s  &&  isSpace(*(pc-1))) pc--;
  429.   *pc = '\0';
  430.   /* no leading spaces: */
  431.   while (*s == ' ')
  432.     {
  433.     for (pc=s;  *pc;  pc++)    *pc = *(pc+1);
  434.  
  435.     }
  436.   /* no double blanks */
  437.   for (;  *s;)
  438.     {
  439.     if (*s == ' '   &&   *(s+1) == ' ')
  440.       {
  441.       for (pc=s;  *pc;  pc++)    *pc = *(pc+1);
  442.  
  443.       }
  444.     else s++;
  445.  
  446.     }
  447.  }
  448. /*
  449. * CleanEnd()
  450. *
  451. * This function cleans up a message trailer for later display via Continue or
  452. * .EH.  Inspired by Glen Heinz (MacCitadel).
  453. */
  454. char *CleanEnd(char *text)
  455.   {
  456.   char *ptr;
  457.   int  wc, lc;    /* Word Count and Letter Count */
  458.   if (strLen(text) == 0) return text;
  459.   ptr = lbyte(text) - 1;      /* End of text of msg */
  460.   /*
  461.   * Strip trailing whitespace.  We structure the loop this
  462.   * way to avoid any chance of accidentally accessing memory outside
  463.   * of the memory area.
  464.   */
  465.   while (ptr != text - 1)
  466.     {
  467.     if (!(*ptr == ' ' || *ptr == NEWLINE || *ptr == TAB)) break;
  468.     ptr--;
  469.  
  470.     }
  471.   ptr++;    /* point at byte following last significant character */
  472.   *ptr = 0;    /* tie it off with a NULL */
  473.   /* Now we want to find a "preferred place" */
  474.   for (wc = lc = 0, ptr--; wc < 4 && ptr > text && lc < 35; ptr--, lc++)
  475.     {
  476.     if (*ptr == ' ') wc++;
  477.     if (*ptr == NEWLINE) break;     /* can't go beyond embedded NEWLINE */
  478.  
  479.     }
  480.   if (ptr == text) return ptr;    /* if msg is empty or < 35 chars long */
  481.   /* else */       return (ptr + 1);    /* else return "favored" spot */
  482.  
  483.   }
  484.